package com.ggh.jinjilive.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.blankj.utilcode.util.BarUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentHomeBinding;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.home.follow.FollowFragment;
import com.ggh.jinjilive.ui.home.live.LiveFragment;
import com.ggh.jinjilive.ui.home.video.VideoFragment;
import com.ggh.jinjilive.ui.home.voice.VoiceFragment;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends BaseFragment<FragmentHomeBinding> {
    //
    private String[] mFragmentTitles = {"直播", "视频","语音派对","关注"};
    //
    private List<Fragment> fragmentList = new ArrayList<>();
    //适配器
    private ProfitOrderAdapter profitOrderAdapter;
    //直播列表
    private LiveFragment liveFragment;
    //视频列表
    private VideoFragment videoFragment;
    //语音房列表
    private VoiceFragment voiceFragment;
    //关注列表
    private FollowFragment followFragment;
    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        super.initView();
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
        fragmentList.clear();
        liveFragment = new LiveFragment();
        fragmentList.add(liveFragment);
        videoFragment = new VideoFragment();
        fragmentList.add(videoFragment);
        voiceFragment = new VoiceFragment();
        fragmentList.add(voiceFragment);
        followFragment = new FollowFragment();
        fragmentList.add(followFragment);
    }

    @Override
    public void initData() {
        super.initData();
        profitOrderAdapter = new ProfitOrderAdapter(getChildFragmentManager());
        binding.orderVp.setAdapter(profitOrderAdapter);
        binding.stlOrder.setViewPager(binding.orderVp, mFragmentTitles);
        binding.orderVp.setCurrentItem(1);
    }


    class ProfitOrderAdapter extends FragmentPagerAdapter {

        public ProfitOrderAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public void destroyItem(@NonNull @NotNull ViewGroup container, int position, @NonNull @NotNull Object object) {
//            super.destroyItem(container, position, object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles[position];
        }

        @Override
        public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            super.setPrimaryItem(container, position, object);
        }
    }
}
